package com.jumi.microservice.controller.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jm.member.common.core.utils.DelFlag;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.request.JmQrcodeSaveRequest;
import com.jumi.microservice.entity.JmQrcode;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.entity.JmUserInvite;
import com.jumi.microservice.service.IJmQrcodeService;
import com.jumi.microservice.service.IJmUserInviteService;
import com.jumi.microservice.service.IJmUserService;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-08-27
 */
@RestController
@RequestMapping("/api/jm-qrcode")
@Api(value = "C-二维码管理", tags = {"C-二维码管理接口"})
public class JmQrcodeController {

    @Autowired
    IJmQrcodeService jmQrcodeService;

    @Autowired
    IJmUserService userService;

    @Autowired
    IJmUserInviteService inviteService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传二维码",tags = "上传二维码")
    public ResponseResult upload(JmQrcodeSaveRequest jmQrcodeSaveRequest){
        Integer memberId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmQrcode qrcode = new JmQrcode();
        BeanUtil.copyProperties(jmQrcodeSaveRequest,qrcode);

        //判断是否已上传过
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_del", DelFlag.normal);
        wrapper.eq("uid",memberId);
        JmQrcode jmQrcode = jmQrcodeService.getOne(wrapper);
        qrcode.setUid(memberId);
        if (ObjectUtil.isNotNull(jmQrcode))
            return this.update(qrcode);
        return this.save(qrcode);

    }


    private ResponseResult save(JmQrcode jmQrcode){
        jmQrcode.setCreateTime(LocalDateTime.now());
        jmQrcode.setUpdateTime(LocalDateTime.now());
        boolean result = jmQrcodeService.save(jmQrcode);
        if (result) return ResponseResult.success();
        return ResponseResult.error();
    }

    private ResponseResult update(JmQrcode jmQrcode){
        jmQrcode.setUpdateTime(LocalDateTime.now());
        boolean result = jmQrcodeService.updateById(jmQrcode);
        if (result) return ResponseResult.success();
        return ResponseResult.error();

    }


    @PostMapping("/getQrcode")
    @ApiOperation(value = "获取二维码")
    public ResponseResult getQrcode() {
        Integer memberId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_del", DelFlag.normal);
        wrapper.eq("uid",memberId);
        JmQrcode jmQrcode = jmQrcodeService.getOne(wrapper);
        if (ObjectUtil.isNotNull(jmQrcode)) return ResponseResult.success(jmQrcode);
        return ResponseResult.success("未上传二维码");
    }

    @PostMapping("/getInviterQrcode")
    @ApiOperation(value = "获取邀请人二维码")
    public ResponseResult getInviterQrcode(@ApiParam(required = true, value = "邀请人id") @RequestParam Integer inviterMemberId,
                                           @ApiParam(required = true, value = "用户id") @RequestParam Integer memberId
    ) {
        //查询用户是否存在上级
        JmUser jmUser = userService.checkUserExist(memberId);//检查个人信息
        JmUserInvite invite = inviteService.getInviteByUid(memberId);//获得上级信息
        if (ObjectUtil.isNotEmpty(invite)) {
            JmQrcode jmQrcode = jmQrcodeService.getUserQrcode(invite.getInviteUid());
            if (ObjectUtil.isNotNull(jmQrcode)) return ResponseResult.success(jmQrcode);
        }
        JmQrcode jmQrcode = jmQrcodeService.getUserQrcode(inviterMemberId);
        if (ObjectUtil.isNotNull(jmQrcode)) return ResponseResult.success(jmQrcode);
        return ResponseResult.success("未上传二维码");
    }

}
